package cn.drenal.capcap.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.drenal.capcap.common.Response;
import cn.drenal.capcap.entity.Artwork;
import cn.drenal.capcap.entity.RecommendedArtwork;
import cn.drenal.capcap.entity.vo.RecommendedArtworkListVO;
import cn.drenal.capcap.service.ArtworkService;
import cn.drenal.capcap.service.RecommendedArtworkService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

/**
 * 推荐作品管理控制器
 */
@RestController
@RequestMapping("/recommend")
@RequiredArgsConstructor
public class RecommendedArtworkController {

    private final RecommendedArtworkService recommendedArtworkService;
    private final ArtworkService artworkService;

    /**
     * 分页查询推荐作品列表
     *
     * @param current 当前页
     * @param size    每页大小
     * @param reason  推荐理由
     * @param status  推荐状态
     * @return 分页数据
     */
    @GetMapping("/page")
    @SaCheckPermission("homepage:recommended:list")
    public Response<Page<RecommendedArtworkListVO>> page(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String reason,
            @RequestParam(required = false) Integer status
    ) {
        Page<RecommendedArtwork> page = recommendedArtworkService.page(current, size, reason, status);
        Page<RecommendedArtworkListVO> voPage = new Page<>();
        BeanUtils.copyProperties(page, voPage);
        voPage.setRecords(page.getRecords().stream().map(recommendedArtwork -> {
            RecommendedArtworkListVO vo = new RecommendedArtworkListVO();
            BeanUtils.copyProperties(recommendedArtwork, vo);
            Artwork artwork = artworkService.getById(recommendedArtwork.getArtworkId());
            if (artwork != null) {
                vo.setArtworkTitle(artwork.getTitle());
                vo.setArtworkCover(artwork.getCoverImage());
            }
            return vo;
        }).toList());
        return Response.success(voPage);
    }

    /**
     * 获取推荐作品详情
     *
     * @param id 推荐作品ID
     * @return 推荐作品详情
     */
    @GetMapping("/{id}")
    @SaCheckPermission("homepage:recommended:list")
    public Response<RecommendedArtwork> getById(@PathVariable Long id) {
        return Response.success(recommendedArtworkService.getById(id));
    }

    /**
     * 新增推荐作品
     *
     * @param recommendedArtwork 推荐作品信息
     * @return 操作结果
     */
    @PostMapping
    @SaCheckPermission("homepage:recommended:add")
    public Response<Void> add(@RequestBody RecommendedArtwork recommendedArtwork) {
        recommendedArtworkService.save(recommendedArtwork);
        return Response.success();
    }

    /**
     * 更新推荐作品
     *
     * @param id                 推荐作品ID
     * @param recommendedArtwork 推荐作品信息
     * @return 操作结果
     */
    @PutMapping("/{id}")
    @SaCheckPermission("homepage:recommended:edit")
    public Response<Void> update(@PathVariable Long id, @RequestBody RecommendedArtwork recommendedArtwork) {
        recommendedArtwork.setId(id);
        recommendedArtworkService.updateById(recommendedArtwork);
        return Response.success();
    }

    /**
     * 删除推荐作品
     *
     * @param id 推荐作品ID
     * @return 操作结果
     */
    @DeleteMapping("/{id}")
    @SaCheckPermission("homepage:recommended:delete")
    public Response<Void> delete(@PathVariable Long id) {
        recommendedArtworkService.removeById(id);
        return Response.success();
    }
}